package com.fuxin.hanfu.controller;

import com.fuxin.hanfu.domain.Item;
import com.fuxin.hanfu.domain.User;
//import com.fuxin.hanfu.service.ItemServive;
import com.fuxin.hanfu.service.UserService;
import com.fuxin.hanfu.uitls.MyResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/list")
    public List<User> findList(){
        return userService.findList();
    }
    /*根据id查询*/
    @GetMapping("/find/{userId}")
    public MyResponse findById(@PathVariable Integer userId) {
        return MyResponse.toJson(true, userService.findById(userId));
    }

    /*新增*/
    @PostMapping("/add")
    public Integer insert(@RequestBody User user) {
        return userService.insert(user);
    }
    /*新增*/
    @PostMapping("/addUser")
    public Integer insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    /*修改*/
    @PutMapping("/update")
    public MyResponse update(@RequestBody User user) {
        return MyResponse.toJson(true, userService.update(user));
    }

    /*删除*/
    @DeleteMapping("/delete/{userId}")
    public Integer delete(@PathVariable Integer userId) {
        return  userService.delete(userId);

    }
    /*登录*/
    @PostMapping("/login")
    public MyResponse login(@RequestBody User user) {
        User foundUser = userService.findByUsernameAndPassword(user.getUsername(), user.getPassword());
        if (foundUser != null) {
            // 生成token（这里只是示例，实际应使用JWT等安全方式）
            String token = UUID.randomUUID().toString();
            // 通常会将token存储到数据库与用户关联，或设置过期时间等
            // 这里简单起见，直接返回token
            return MyResponse.toJson(true, token);
        } else {
            return MyResponse.toJson(false, "用户名或密码错误");
        }
    }
}
